<?php
/**
 * Author: zhao-baolin
 * E-mail: qiutian@fengyumeng.com
 */

namespace System;


class Input
{
    public $method = '';
    public $is_get = false;
    public $is_post = false;
    public $is_ajax = false;

    public function __construct()
    {
        $this->get_request_method();
        Loader::load_class(__CLASS__,$this);
    }

    public function method()
    {
        return $this->method;
    }

    public function is_get()
    {
        return $this->is_get;
    }

    public function is_post()
    {
        return $this->is_post;
    }

    public function is_ajax()
    {
        return $this->is_ajax;
    }


    private function get_request_method()
    {
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
            $this->method = 'AJAX';
        }else{
            $this->method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD'];
        }
        switch($this->method){
            case 'GET':
                $this->is_get = true;
                break;
            case 'POST':
                $this->is_post = true;
                break;
            case 'AJAX':
                $this->is_ajax = true;
                break;
            default:
                break;
        }
    }

    public function post($Key = '',$filter = false)
    {
        return $this->get_request_param($_POST,$Key,$filter);
    }

    public function get($Key = '',$filter = false)
    {
        return $this->get_request_param($_GET,$Key,$filter);
    }

    private function get_request_param(&$arr,$key,$filter)
    {
        $param = null;
        if('' == $key){
            return $arr;
        }else{
            if(is_array($key)){
                foreach($key as $k){
                    $param[$k] = $filter ? $this->filtration_data($arr[$k]) : $arr[$k];
                }
            }
            if(is_string($key)){
                $param =  $filter ? $this->filtration_data($arr[$key]) : $arr[$key];
            }
        }

        return $param;
    }

    //数据过滤
    public function filtration_data($data)
    {
        str_replace([' ','#','$'],'',$data);
        addslashes($data);
        return $data;
    }

}